package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.finance.domain.TSalaryBillInfo;
import com.ruoyi.finance.service.ITSalaryBillInfoService;
import com.ruoyi.finance.service.ITSalaryWayBillRelService;
import com.ruoyi.waybill.domain.TWayBillExt;
import com.ruoyi.waybill.service.ITWayBillExtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.time.LocalDate;

import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 定时任务，工资账单，任务
 * 
 * @author ruoyi
 */
@Component("sbTask")
public class SbTask
{
    @Autowired
    private ITWayBillExtService tWayBillExtService;

    @Autowired
    private ITSalaryBillInfoService tSalaryBillInfoService;

    @Autowired
    private ITSalaryWayBillRelService tSalaryWayBillRelService;

    private static Logger logger = LoggerFactory.getLogger(SbTask.class);

    /**
     * 统计月工资单，无参
     */

    public void statisticMonthSalaryBill()
    {
        logger.debug("统计，月工资单，月账单，按车牌号，开始 ...");
        /**
         * 计算当前日期，前一月日期
         */
        LocalDate currentDate = LocalDate.now();
        logger.debug("当前日期: " + currentDate);
        LocalDate lastMonthDate = currentDate.minusMonths(1);
        logger.debug("上月日期: " + lastMonthDate);
        LocalDate lastMonthFirstDay = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        logger.debug("上个月第一天：" + lastMonthFirstDay);
        LocalDate lastMonthLastDay = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        logger.debug("上个月最后一天：" + lastMonthLastDay);

        TWayBillExt tWayBillExt = new TWayBillExt();
        Double monthSalaryTotal = 0.0;
        TSalaryBillInfo tSalaryBillInfo = new TSalaryBillInfo();
        String monthSalaryBillNumber;
        Long monthSalaryBillId = 0L;
        List<Long> wayBillIds = new ArrayList<>();

        /**
         * 按录单日期，筛选车牌号码列表
         */
        List<String> vehicleLicenseNumbers = tWayBillExtService.selectStatisticFieldByRecordDate(0 /*车牌号码*/, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
//        logger.debug("车牌号码列表：vehicleLicenseNumbers = " + vehicleLicenseNumbers);

        /**
         * 遍历车牌号，运单
         */
        for(String vehicleLicenseNumber : vehicleLicenseNumbers)
        {
            /**
             * 按车牌号。录单日期，查询运单
             */
            tWayBillExt.setVehicleLicenseNumber(vehicleLicenseNumber);
            logger.debug("车牌号码：vehicleLicenseNumber = " + vehicleLicenseNumber);
            List<TWayBillExt> wayBillExts = tWayBillExtService.selectTWayBillExtListUsedByMonthStatistic(0 /*车牌号码*/, tWayBillExt, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
//            logger.debug("运单列表：wayBillExts = " + wayBillExts);
            /**
             * 合计工资，记录运单ID
             */
            for(TWayBillExt wayBillExt : wayBillExts) {
                monthSalaryTotal = monthSalaryTotal + wayBillExt.getSalaryAmount();
                wayBillIds.add(wayBillExt.getWayBillId());
            }

            /**
             * 保护，goodsIds，为空，直接退出
             */
            if(wayBillIds.isEmpty()) return;

            /**
             * 月工资单编号
             */
            monthSalaryBillNumber = OrderUtils.getOrderNumber("MS", "-");

            /**
             * 工资总额，四舍五入，保留2位小数
             */
            monthSalaryTotal = Math.round(monthSalaryTotal * 100.0) / 100.0;

            /**
             * 月工资单，数据准备
             */
            tSalaryBillInfo.setSalaryBillNumber(monthSalaryBillNumber);
            tSalaryBillInfo.setSalaryBillDate(Date.valueOf(lastMonthDate));
            tSalaryBillInfo.setVehicleLicenseNumber(vehicleLicenseNumber);
            tSalaryBillInfo.setDriverName(wayBillExts.get(0).getDriverName());
            tSalaryBillInfo.setContactMobile(wayBillExts.get(0).getContactMobile());
            tSalaryBillInfo.setSalaryTotal(monthSalaryTotal);
            tSalaryBillInfo.setSalaryPaymentStatus("未付款");
            tSalaryBillInfo.setStatisticField("车牌号码");
            tSalaryBillInfo.setStatisticGranularity("月账单");
            tSalaryBillInfo.setCreateBy("system");
            tSalaryBillInfo.setCreateTime(DateUtils.getNowDate());

            /**
             * 月工资单，插入数据
             */
            tSalaryBillInfoService.insertTSalaryBillInfo(tSalaryBillInfo);
            sleep(300);

            /**
             * 获取插入记录，ID
             */
            monthSalaryBillId = tSalaryBillInfoService.selectSalaryBillIdBySalaryBillNumber(monthSalaryBillNumber);
//            logger.debug("OOO " + "salaryBillId = " + salaryBillId);

            /**
             * monthSalaryTotal清零
             */
            monthSalaryTotal = 0.0;


            /**
             * 工资运单关联表，数据插入
             */
//            logger.debug("OOO " + "wayBillIds = " + wayBillIds);
            tSalaryWayBillRelService.insertTSalaryWayBillRelExt(monthSalaryBillId, wayBillIds);
            wayBillIds.clear();
        }

        logger.debug("统计，月工资单，月账单，按车牌号，结束 ...");
    }

}
